<template>
  <div>
    <TopFilter :list="topFilterList" :cols="3" :isSubmitBtn="false" :collapse="false" />
    <FilterTable
      ref="itemTimesModelTable"
      :columns="columns"
      :fetchapi="getLabour"
      columnsRef="itemTimesModelTableTableL"
      :params="fetchParamsLabour"
      selectionType="single"
      :onCellChange="getColums"
      height="auto"
      :datakey="'rows'"
      :onRowSelectChange="columns => (this.selectColumns = columns)"
      :onColumnsChange="columns => (this.columns = columns)"
      :onSyncTableData="columnsChange"
    >
      <template slot="controls">
        <el-button size="small" type="primary" @click="addCloums">新增</el-button>
        <el-button size="small" type="primary" :disabled="this.deleteType" @click="deleteCloums">删除</el-button>
      </template>
    </FilterTable>
    <span> 每天平均派工工时<el-input v-model="hour" :style="{ width: '50px' }" :disabled="true"></el-input>每天平均派工工时=实际工作工时/派工天数 </span>
    <div style="text-align:center;" class="dialogBottmView">
      <el-button size="small" type="primary" @click="saveItmeTime">确定</el-button>
      <el-button size="small" type="primary" @click="closePage(true)">取消</el-button>
    </div>
  </div>
</template>

<script>
import { svaeRoAssignItem } from '@/api/repair/repairAssign/workAdjust';
import { queryRoAssignItem } from '@/api/repair/repairAssign/repairAssign';
import { getDealerUser } from '@/api/repair/repairReception/customer';
export default {
  name: 'itemTimesModel',
  props: ['id'],
  data() {
    this.myTable = null;
    return {
      fetchParamsLabour: {},
      itemTableColumns: [],
      selectColumns: [],
      checkDate: false,
      deleteType: true,
      hour: 0,
      topFilterList: this.createTableColumnsLabour(),
      columns: this.createTableColumnsHour()
    };
  },
  watch: {
    selectColumns(val) {
      if (val.length > 0) {
        if (this.selectColumns[0].ITEM_ID) {
          this.deleteType = true;
        } else {
          this.deleteType = false;
        }
      }
    }
  },
  mounted() {
    this.myTable = this.$refs.itemTimesModelTable.$refs.pageTable;
    this.initFormSelectData();
  },
  methods: {
    createTableColumnsLabour() {
      return [
        {
          type: 'INPUT',
          label: '派工工时',
          fieldName: 'labourHour',
          placeholder: '请填写派工工时',
          initialValue: this.id.ASSIGN_LABOUR_HOUR,
          disabled: true
        },
        {
          type: 'INPUT',
          label: '技师代码',
          fieldName: 'technicianCode',
          placeholder: '请填写技师代码',
          initialValue: this.id.TECHNICIAN,
          disabled: true
        },
        {
          type: 'SELECT',
          label: '技师',
          fieldName: 'technician',
          placeholder: '请填写技师',
          itemList: [],
          initialValue: this.id.TECHNICIAN,
          disabled: true
        }
      ];
    },
    createTableColumnsHour() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 100,
          sorter: true,
          render: props => {
            return <span>{props.$index + 1}</span>;
          }
        },
        {
          title: '工作日期',
          dataIndex: 'WORKING_DAY',
          sorter: true,
          filter: true,
          width: 150,
          editable: true,
          editType: 'date-picker',
          editRequired: true,
          dateFormat: 'yyyy-MM-dd'
        },
        {
          title: '实际工作工时',
          dataIndex: 'FACT_LABOUR_HOUR',
          sorter: true,
          filter: true,
          width: 150,
          min: 0,
          editable: true,
          editType: 'number',
          editRequired: true
        }
      ];
    },
    async getLabour() {
      let res = await queryRoAssignItem(this.id.ASSIGN_ID);
      return res;
    },
    getColums() {
      this.contrastDate(this.itemTableColumns);
      this.hour = this.countHour();
    },
    async initFormSelectData() {
      const res = await getDealerUser();
      this.topFilterList.find(x => x.fieldName === 'technician').itemList = res.data.map(x => ({ text: x.employeeName, value: x.userId + '' }));
    },
    countHour() {
      let a = 0;
      let b = +this.itemTableColumns.length;
      this.itemTableColumns.forEach(x => {
        a = +x.FACT_LABOUR_HOUR + a;
      });
      return a / b;
    },
    contrastDate(val) {
      if (val.length > 1) {
        for (let i = 0; i < val.length; i++) {
          for (let j = 0; j < val.length; j++) {
            if (i != j) {
              if (val[i].WORKING_DAY != '' && val[j].WORKING_DAY != '') {
                this.checkDate = val[i].WORKING_DAY == val[j].WORKING_DAY;
                if (this.checkDate) {
                  this.$notify.error({ title: '提示信息', message: '工作日期已存在!' });
                  return;
                }
              }
            }
          }
        }
      }
    },
    addCloums() {
      let obj = {
        FACT_LABOUR_HOUR: '1'
      };
      this.myTable.EXECUTE_INSERT(obj);
    },
    deleteCloums() {
      this.myTable.EXECUTE_DELETE();
    },
    async saveItmeTime() {
      let a = true;
      let b = 0;

      this.countHour();
      this.itemTableColumns.forEach(x => {
        if (!x.WORKING_DAY) {
          // this.$notify.error({ title: '提示信息', message: '工作日期不能为空!' });
          b++;
          a = false;
          return;
        }
      });
      if (b > 0) {
        this.$notify.error({ title: '提示信息', message: '工作日期不能为空!' });
        return;
      }
      this.contrastDate(this.itemTableColumns);
      if (a) {
        if (!this.checkDate) {
          let obj = {
            roNo: this.id.RO_NO,
            assignId: this.id.ASSIGN_ID,
            technicianCode: this.id.TECHNICIAN,
            tabList: this.itemTableColumns
          };
          let res = await svaeRoAssignItem(obj);
          if (res.resultCode == 200) {
            this.closePage(false);
          }
        }
      }
    },
    closePage(val) {
      this.$emit('close', val);
    },
    columnsChange(val) {
      this.itemTableColumns = val;
      let a = 0;
      let b = +val.length;
      val.forEach(x => {
        a = +x.FACT_LABOUR_HOUR + a;
      });
      if (b == 0) {
        this.hour = 0;
      } else {
        this.hour = a / b;
      }
    }
  }
};
</script>
